﻿using UnityEngine;
using System.Collections;
using System;
using HDJ.Framework.Core;
public class CharacterCombatCollisionRegion : MonoBehaviour
{
    /// <summary>
    /// 头部
    /// </summary>
    public SphereCollisionRegion head_Reg;
    /// <summary>
    /// 身体
    /// </summary>
    public CapsuleCollisionRegion body_Reg;
    /// <summary>
    /// 左臂第一部分
    /// </summary>
    public CapsuleCollisionRegion left_Arm0_Reg;
    /// <summary>
    /// 左臂第二部分
    /// </summary>
    public CapsuleCollisionRegion left_Arm1_Reg;
    /// <summary>
    /// 左手
    /// </summary>
    public SphereCollisionRegion left_Hand_Reg;
    /// <summary>
    /// 右臂第一部分
    /// </summary>
    public CapsuleCollisionRegion right_Arm0_Reg;
    /// <summary>
    /// 右臂第二部分
    /// </summary>
    public CapsuleCollisionRegion right_Arm1_Reg;
    /// <summary>
    /// 右手
    /// </summary>
    public SphereCollisionRegion right_Hand_Reg;
    /// <summary>
    /// 左腿第一部分
    /// </summary>
    public CapsuleCollisionRegion left_Leg0_Reg;
    /// <summary>
    /// 左腿第二部分
    /// </summary>
    public CapsuleCollisionRegion left_Leg1_Reg;
    /// <summary>
    /// 左脚
    /// </summary>
    public SphereCollisionRegion left_Foot_Reg;

    public CapsuleCollisionRegion right_Leg0_Reg;
    public CapsuleCollisionRegion right_Leg1_Reg;

    public SphereCollisionRegion right_Foot_Reg;

    // Update is called once per frame
    void Update()
    {
        CreateSphereCollision(head_Reg, BodyCollisionRegionType.head);
        CreateSphereCollision(left_Hand_Reg, BodyCollisionRegionType.left_Hand);
        CreateSphereCollision(right_Hand_Reg, BodyCollisionRegionType.right_Hand);
        CreateSphereCollision(left_Foot_Reg, BodyCollisionRegionType.left_Foot);
        CreateSphereCollision(right_Foot_Reg, BodyCollisionRegionType.right_Foot);

        CreateCapsule(body_Reg, BodyCollisionRegionType.Body);
        CreateCapsule(left_Arm0_Reg, BodyCollisionRegionType.left_Arm0);
        CreateCapsule(left_Arm1_Reg, BodyCollisionRegionType.left_Arm1);
        CreateCapsule(right_Arm0_Reg, BodyCollisionRegionType.right_Arm0);
        CreateCapsule(right_Arm1_Reg, BodyCollisionRegionType.right_Arm1);

        CreateCapsule(left_Leg0_Reg, BodyCollisionRegionType.left_Leg0);
        CreateCapsule(left_Leg1_Reg, BodyCollisionRegionType.left_Leg1);
        CreateCapsule(right_Leg0_Reg, BodyCollisionRegionType.right_Leg0);
        CreateCapsule(right_Leg1_Reg, BodyCollisionRegionType.right_Leg1);
    }

    void OnDrawGizmos()
    {
        CreateSphereCollision(head_Reg, BodyCollisionRegionType.head);
        CreateSphereCollision(left_Hand_Reg, BodyCollisionRegionType.left_Hand);
        CreateSphereCollision(right_Hand_Reg, BodyCollisionRegionType.right_Hand);
        CreateSphereCollision(left_Foot_Reg, BodyCollisionRegionType.left_Foot);
        CreateSphereCollision(right_Foot_Reg, BodyCollisionRegionType.right_Foot);

        CreateCapsule(body_Reg, BodyCollisionRegionType.Body);
        CreateCapsule(left_Arm0_Reg,  BodyCollisionRegionType.left_Arm0);
        CreateCapsule(left_Arm1_Reg,  BodyCollisionRegionType.left_Arm1);
        CreateCapsule(right_Arm0_Reg,  BodyCollisionRegionType.right_Arm0);
        CreateCapsule(right_Arm1_Reg,  BodyCollisionRegionType.right_Arm1);

        CreateCapsule(left_Leg0_Reg,   BodyCollisionRegionType.left_Leg0);
        CreateCapsule(left_Leg1_Reg,  BodyCollisionRegionType.left_Leg1);
        CreateCapsule(right_Leg0_Reg,  BodyCollisionRegionType.right_Leg0);
        CreateCapsule(right_Leg1_Reg,  BodyCollisionRegionType.right_Leg1);
    }

    private const string CharacterCollisionsRootName = "CharacterCollisions";
    private Transform root;
    private void CreateCapsule(CapsuleCollisionRegion data, BodyCollisionRegionType bodyCollisionRegionType)
    {
        if (data.Start_Obj == null || data.End_Obj == null)
            return;
        Vector3 dis = data.End_Obj.transform.position - data.Start_Obj.transform.position;
        Vector3 center = data.Start_Obj.transform.position + dis / 2;

        if (data.coliderTrans == null)
        {
            if(root==null)
              root= transform.Find(CharacterCollisionsRootName);
            if(root==null)
            {
                root = new GameObject(CharacterCollisionsRootName).transform;
                root.SetParent(transform);
                root.localPosition = Vector3.zero;
            }
            data.coliderTrans = root.Find(bodyCollisionRegionType.ToString());
            if(data.coliderTrans==null)
            data.coliderTrans = new GameObject(bodyCollisionRegionType.ToString()).transform;
            data.coliderTrans.SetParent(root);
        }
        if (data.collider == null)
        {
            data.collider = data.coliderTrans.GetComponent<CapsuleCollider>();
            if(data.collider==null)
            data.collider = data.coliderTrans.gameObject.AddComponent<CapsuleCollider>();
            data.collider.direction = 1;
            data.collider.height = dis.magnitude+data.heightOffset;
            data.collider.radius = data.radius;
            data.collider.isTrigger = true;
            data.bodyCollisionRegionType = bodyCollisionRegionType;
        }
        if (!Application.isPlaying)
        {
            data.collider.radius = data.radius;
            data.collider.height = dis.magnitude + data.heightOffset;

            if (data.Start_Obj.transform.root != transform.root)
            {
                Transform[] tras = UnityExtendUtils.GetDeepChildTransforms(transform, data.Start_Obj.name);
                if (tras.Length>0)
                    data.Start_Obj = tras[0].gameObject;

                tras = tras = UnityExtendUtils.GetDeepChildTransforms(transform, data.End_Obj.name);
                if (tras .Length>0)
                    data.End_Obj = tras[0].gameObject;
            }
        }
        data.coliderTrans.position = center;
        data.coliderTrans.up =dis.normalized;

    }
    private void CreateSphereCollision(SphereCollisionRegion data, BodyCollisionRegionType bodyCollisionRegionType)
    {
        if (data.center_Obj == null)
            return;

        Vector3 center = data.center_Obj.transform.position + data.offset;
        if (data.coliderTrans == null)
        {
            if (root == null)
                root = transform.Find(CharacterCollisionsRootName);
            if (root == null)
            {
                root = new GameObject(CharacterCollisionsRootName).transform;
                root.SetParent(transform);
                root.localPosition = Vector3.zero;
            }
            data.coliderTrans = root.Find(bodyCollisionRegionType.ToString());
            if (data.coliderTrans == null)
                data.coliderTrans = new GameObject(bodyCollisionRegionType.ToString()).transform;
            data.coliderTrans.SetParent(root);
        }
        if (data.collider == null)
        {
            data.collider = data.coliderTrans.GetComponent<SphereCollider>();
            if (data.collider == null)
                data.collider = data.coliderTrans.gameObject.AddComponent<SphereCollider>();
            data.collider.radius = data.radius;
            data.collider.isTrigger = true;
            data.bodyCollisionRegionType = bodyCollisionRegionType;
        }
        if (!Application.isPlaying)
        {
            data.collider.radius = data.radius;

            if (data.center_Obj.transform.root != transform.root)
            {
                Transform[] tras = UnityExtendUtils.GetDeepChildTransforms (transform,data.center_Obj.name);
                if (tras.Length>0)
                    data.center_Obj = tras[0].gameObject;
            }
        }
        data.coliderTrans.position = center;
    }
 
    [Serializable]
    public class SphereCollisionRegion
    {
        public GameObject center_Obj;
        public Vector3 offset;
        public float radius = 0.1f;

        [NonSerialized]
        public BodyCollisionRegionType bodyCollisionRegionType;
        [NonSerialized]
        public SphereCollider collider;
        [NonSerialized]
        public Transform coliderTrans;
    }

    [Serializable]
    public class CapsuleCollisionRegion
    {

        public GameObject Start_Obj;
        public GameObject End_Obj;

        public float radius = 0.1f;
        public float heightOffset = 0f;
        /// <summary>
        /// 0表示胶囊的高度沿着x轴；1表示胶囊的高度沿着y轴；2表示胶囊的高度沿着z轴。
        /// </summary>
        //public int direction = 0;
        [NonSerialized]
        public BodyCollisionRegionType bodyCollisionRegionType;
        [NonSerialized]
        public CapsuleCollider collider;
        [NonSerialized]
        public Transform coliderTrans;
    }

}

public enum BodyCollisionRegionType
{
    head = 2,
    left_Hand = 4,
    right_Hand = 8,
    left_Foot = 16,
    right_Foot = 32,
    Body =64,
    left_Arm0=128,
    left_Arm1=256,
    right_Arm0 = 512,
    right_Arm1 = 1024,
    left_Leg0=2048,
    left_Leg1= 4096,
    right_Leg0=  8192,
    right_Leg1=   16384,

}
